package com.qlc_demo_api.model;

import java.util.HashMap;
import java.util.Map;

public class ResponseData<T> {

    // 状态码
    private int status;

    // 提示信息
    private String message;

    // 封装有效得数据
    private T data;

    // 成功
    public static ResponseData<Map<String, Object>> success() {
        ResponseData<Map<String, Object>> result = new ResponseData<>();
        result.setStatus(200);
        result.setMessage("success");
        return result;
    }

    // 失败
    public static ResponseData<Map<String, Object>> fail(String message) {
        ResponseData<Map<String, Object>> result = new ResponseData<>();
        String msg = message.isEmpty() ? "fail" : message;
        result.setStatus(400);
        result.setMessage(msg);
        return result;
    }

    // 没有权限
    public static ResponseData<Map<String, Object>> noPermission() {
        ResponseData<Map<String, Object>> result = new ResponseData<>();
        result.setStatus(401);
        result.setMessage("no permission");
        return result;
    }

    // 服务端错误
    public static ResponseData<Map<String, Object>> error() {
        ResponseData<Map<String, Object>> result = new ResponseData<>();
        result.setStatus(500);
        result.setMessage("error");
        return result;
    }

    // 其他异常情况
    public static ResponseData<Map<String, Object>> code(int code) {
        ResponseData<Map<String, Object>> result = new ResponseData<>();
        result.setStatus(code);
        result.setMessage("exception");
        return result;
    }

    // 给data中填充返回的数据
    public ResponseData<T> add(String key, Object value) {
        this.setData(key, value);
        return this;
    }
    public ResponseData<T> add(T map) {
        this.setData(map);
        return this;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
    public void setData(String key, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, value);
        this.data = (T) map;
    }
}
